unit InputDialog;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, BaseDescDialog, cxGraphics, cxLookAndFeels,
  cxLookAndFeelPainters, Menus, cxControls, cxContainer, cxEdit,
  dxSkinsCore, dxSkinsDefaultPainters, cxLabel, cxTextEdit, StdCtrls,
  ExtCtrls, cxCurrencyEdit, cxMaskEdit, cxSpinEdit,
  cxDropDownEdit, cxCalendar, dxSkinsForm, ComCtrls, dxCore, cxDateUtils,
  cxButtons;

type
  TEditClass = class of TEdit;

  TfrmInputDialog = class(TfrmBaseDescDialog)
    lblInput: TcxLabel;
    cxTextEdit1: TcxTextEdit;
    cxDateEdit1: TcxDateEdit;
    cxSpinEdit1: TcxSpinEdit;
    cxCurrencyEdit1: TcxCurrencyEdit;
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure SetCaption(sCaption, FuncName: string);
  end;

function GetInputPwd(DefValue: string = ''; Caption: string = ''; FuncName: string = ''): string;  
function GetInputText(DefValue: string = ''; Caption: string = ''; FuncName: string = ''): string;
function GetInputDouble(DefValue: Double = 0.00; Dec: integer = 8; Caption: string = ''; FuncName: string = ''): Double;
function GetInputInteger(DefValue: Integer = 0; Caption: string = ''; FuncName: string = ''): integer;
function GetInputDate(DefValue: TDateTime; Caption: string = ''; FuncName: string = ''): TDateTime;

var
  frmInputDialog: TfrmInputDialog;

implementation

{$R *.dfm}

function GetInputPwd(DefValue: string = ''; Caption: string = ''; FuncName: string = ''): string;
begin
  Result := '';
  frmInputDialog := TfrmInputDialog.Create(nil);
  frmInputDialog.SetCaption(Caption, FuncName);
  frmInputDialog.cxTextEdit1.Properties.EchoMode := eemPassword;
  frmInputDialog.cxTextEdit1.Visible := True;
  frmInputDialog.cxTextEdit1.Text := DefValue;
  if frmInputDialog.ShowModal = mrOk then
    Result := Trim(frmInputDialog.cxTextEdit1.Text);
  if frmInputDialog <> nil then
    FreeAndNil(frmInputDialog);
end;

function GetInputText(DefValue: string = ''; Caption: string = ''; FuncName: string = ''): string;
begin
  Result := '';
  frmInputDialog := TfrmInputDialog.Create(nil);
  frmInputDialog.SetCaption(Caption, FuncName);
  frmInputDialog.cxTextEdit1.Visible := True;
  frmInputDialog.cxTextEdit1.Text := DefValue;
  if frmInputDialog.ShowModal = mrOk then
    Result := Trim(frmInputDialog.cxTextEdit1.Text);
  if frmInputDialog <> nil then
    FreeAndNil(frmInputDialog);
end;

function GetInputDouble(DefValue: Double = 0.00; Dec: integer = 8; Caption: string = ''; FuncName: string = ''): Double;
begin
  Result := 0;
  frmInputDialog := TfrmInputDialog.Create(nil);
  frmInputDialog.SetCaption(Caption, FuncName);
  frmInputDialog.cxCurrencyEdit1.Visible := True;
  frmInputDialog.cxCurrencyEdit1.Value := DefValue;
  frmInputDialog.cxCurrencyEdit1.Properties.DecimalPlaces := Dec;
  if frmInputDialog.ShowModal = mrOk then
    Result := frmInputDialog.cxCurrencyEdit1.Value;
  if frmInputDialog <> nil then
    FreeAndNil(frmInputDialog);
end;

function GetInputInteger(DefValue: Integer = 0; Caption: string = ''; FuncName: string = ''): integer;
begin
  Result := 0;
  frmInputDialog := TfrmInputDialog.Create(nil);
  frmInputDialog.SetCaption(Caption, FuncName);
  frmInputDialog.cxSpinEdit1.Visible := True;
  frmInputDialog.cxSpinEdit1.Value := DefValue;
  if frmInputDialog.ShowModal = mrOk then
    Result := frmInputDialog.cxSpinEdit1.Value;
  if frmInputDialog <> nil then
    FreeAndNil(frmInputDialog);
end;

function GetInputDate(DefValue: TDateTime; Caption: string = ''; FuncName: string = ''): TDateTime;
begin
  Result := 0;
  frmInputDialog := TfrmInputDialog.Create(nil);
  frmInputDialog.SetCaption(Caption, FuncName);
  frmInputDialog.cxDateEdit1.Visible := True;
  if DefValue = 0 then
    frmInputDialog.cxDateEdit1.Date := Now()
  else
    frmInputDialog.cxDateEdit1.Date := DefValue;
  if frmInputDialog.ShowModal = mrOk then
    Result := frmInputDialog.cxDateEdit1.Date;
  if frmInputDialog <> nil then
    FreeAndNil(frmInputDialog);
end;

{ TfrmInputDialog }

procedure TfrmInputDialog.SetCaption(sCaption, FuncName: string);
begin
  if sCaption <> '' then
    Caption := sCaption;
  if FuncName <> '' then
  begin
    labFuncName.Visible := True;
    labFuncName.Caption := FuncName;
  end;
end;

procedure TfrmInputDialog.FormShow(Sender: TObject);
begin
  inherited;
  if cxDateEdit1.Visible and cxDateEdit1.CanFocus then
    cxDateEdit1.SetFocus
  else if cxSpinEdit1.Visible and cxSpinEdit1.CanFocus then
    cxSpinEdit1.SetFocus
  else if cxCurrencyEdit1.Visible and cxCurrencyEdit1.CanFocus then
    cxCurrencyEdit1.SetFocus
  else if cxTextEdit1.Visible and cxTextEdit1.CanFocus then
    cxTextEdit1.SetFocus;
end;

end.

